using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ElfTyping.States;
using ElfTyping.General;
using ElfTyping.States.GameState;
using ElfTyping.States.CampState;
using ElfTyping.States.StoryState;

namespace ElfTyping
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TypingGame : Microsoft.Xna.Framework.Game
    {
        State currentState;
        Camera camera;
        InputInfo inputInfo = new InputInfo();


        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private TypingGame()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;

            Window.Title = "FETEB";
            
            Content.RootDirectory = "Content";
        }

        private static TypingGame singleton = null;
        public static TypingGame Game()
        {
            if (singleton == null)
            {
                singleton = new TypingGame();
            }

            return singleton;
        }

        

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();

            DictionaryManager.GetManager().LoadAllDictionaries();
            PlayerStatus.GetStatus().LoadFromXml();
            
            if (PlayerStatus.GetStatus().FurthestChapter == 0)
            {
                currentState = new StoryState(0, 1);
            }
            else
            {
                currentState = new TitleScreenState();
            }

            //currentState = new GameState(1,1);
            //currentState = new EndOfChapterState(new GameState(1,1), true);

            // cheat a bit
            currentState.InitializeState();

            //            
            //Cards.CardManager.GetManager().FoundBooster(Cards.CardManager.GetManager().GenerateBooster());
            //Cards.CardManager.GetManager().FoundBooster(Cards.CardManager.GetManager().GenerateBooster());
            //Cards.CardManager.GetManager().FoundBooster(Cards.CardManager.GetManager().GenerateBooster());
            //Cards.CardManager.GetManager().FoundBooster(Cards.CardManager.GetManager().GenerateBooster());
            //PuzzlePieceManager.GetManager().FoundPiece(PuzzlePieceManager.GetManager().GenerateRandomPuzzlePiece());
            //PuzzlePieceManager.GetManager().FoundPiece(PuzzlePieceManager.GetManager().GenerateRandomPuzzlePiece());
            //PlayerStatus status = PlayerStatus.GetStatus();
            //status.CurrentEquipment = new Equipment();
            //status.CurrentEquipment.Arrow = new Cards.ArrowCard(2);
            //status.CurrentEquipment.Passive1 = new Cards.PassiveCard(new ElfTyping.States.GameState.PlayerPassives.DoubleShotPassive());
            //status.CurrentEquipment.Spell1 = new Cards.SpellCard(new States.GameState.PlayerSpells.HealSpell());
            //status.GainExperience(160);
            //status.CurrentChapter = 5;
            //status.CurrentQuest = 1;
            //status.FurthestChapter = 8;
            //status.FurthestQuest = 1;
            //status.Gold = 69;
            //status.SaveToXml();
                        
            //Exit();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            camera = new Camera(spriteBatch);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            
            inputInfo.Tick(dt);
            currentState.HandleInput(inputInfo);

            currentState.Tick(dt);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            currentState.Draw(camera);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void ChangeState(State nextState)
        {
            currentState = nextState;
            currentState.InitializeState();
        }
    }
}
